package com.example.smartlearning.controller;

import com.example.smartlearning.config.Annontation;
import com.example.smartlearning.service.impl.AIServiceImpl;
import com.example.smartlearning.service.impl.UserServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("/Smart/User/")
public class UserController {
    @Resource
    UserServiceImpl userService;
    @Resource
    AIServiceImpl aiService;

    /**
     * @param account 账号
     * @param pwd     密码
     * @param request 请求地址
     * @return 登录方法
     */
//    @Annontation.LimitRequest(count = 5)
    @GetMapping("login")
    public String login(@RequestParam("account") String account, @RequestParam("pwd") String pwd, HttpServletRequest request) {
        return userService.logic(account, pwd, request);
    }

    /**
     * @param account 账号
     * @param pwd     密码
     * @return 登录方法
     */
    @PostMapping("register")
    public String register(@RequestParam("account") String account, @RequestParam("pwd") String pwd) {
        return userService.register(account, pwd);
    }

    /**
     * 根据用户id获取用户详情
     *
     * @param userId 用户id
     * @return 是否成功
     */
    @GetMapping("getUser")
    public String getUser(@RequestParam("userId") int userId) {
        return userService.getUser(userId);
    }

    /**
     * 根据用户id更新用户详情
     *
     * @param userId   用户id
     * @param nickName 别名
     * @param number   学号
     * @param avatar   头像
     * @param email    邮箱
     * @param interest 兴趣
     * @param grade    年级
     * @param request  请求
     * @return 返回结果
     */
    @PutMapping("updateUser")
    public String updateUser(
            @RequestParam("userId") int userId,
            @RequestParam("nickName") String nickName,
            @RequestParam("number") String number,
            @RequestParam("avatar") String avatar,
            @RequestParam("email") String email,
            @RequestParam("phone") String phone,
            @RequestParam("interest") String interest,
            @RequestParam("grade") int grade,
            HttpServletRequest request
    ) {
        return userService.updateUser(userId, nickName, number, avatar, email, phone, interest, grade, request);
    }

    /**
     * 更新密码接口
     *
     * @param account 账号
     * @param pwd     密码
     * @param newPwd  新密码
     * @return 返回
     */
    @PutMapping("updatePwd")
    public String updatePwd(@RequestParam("account") String account, @RequestParam("pwd") String pwd, @RequestParam("newPwd") String newPwd) {
        return userService.updatePwd(account, pwd, newPwd);
    }

    /**
     * 获取用户点赞列表且分页
     *
     * @param userId 用户id
     * @param start  开始位置
     * @param limit  数据条数
     * @return 返回
     */
    @PostMapping("getUpvoteList")
    public String getUpvoteList(@RequestParam("userId") int userId, @RequestParam("start") int start, @RequestParam("limit") int limit) {
        return userService.getUpvoteList(userId, start, limit);
    }

    /**
     * 用户开通vip
     *
     * @param userId 用户id
     * @return 返回
     */
    @PostMapping("liberalVip")
    public String liberalVip(@RequestParam("userId") int userId) {
        return userService.liberalVip(userId);
    }

    /**
     *
     * @param userId 用户id
     * @param file 上传头像
     * @return 返回
     */
    @PostMapping("updateUserAvatarInt")
    public String updateUserAvatarInt(@RequestParam("userId") int userId, @RequestParam("file") MultipartFile file) {
        String avatar = aiService.upFile(file);
        return userService.updateUserAvatarInt(userId, avatar);
    }

    @PostMapping("selectFace")
    public String selectFace(@RequestParam("name") String name) {
        return userService.selectFace(name);
    }

    @PostMapping("addFace")
    public String addFace(@RequestParam("userId") int userId,@RequestParam("name") String name,@RequestParam("file") MultipartFile file) {
        String url = aiService.upFile(file);
        return userService.addFace(userId,name,url);
    }


}
